#include "framelessdialog.h"
#include "qrcutil.h"
#include "ui_framelessdialog.h"

#include <QGraphicsEffect>
#include <moveeater.h>

FramelessDialog::FramelessDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FramelessDialog)
    , m_widget(new QWidget)
{
    // 设置无边框+透明
    setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setWindowFlag(Qt::WindowContextHelpButtonHint, false);

    QFont font = qApp->font();
    font.setBold(true);
    font.setPointSize(12);

    ui->setupUi(m_widget);
    ui->title->setFont(font);
    ui->closeButton->setIcon(QIcon(":/frameless/qrc/pop_close.png"));
    ui->closeButton->setIconSize(QSize(30, 25));

    connect(ui->closeButton, &QPushButton::clicked, this, &FramelessDialog::close);

    new MoveEater(this);
}

FramelessDialog::~FramelessDialog()
{
    delete ui;
}

void FramelessDialog::setupUi(QWidget *frame)
{
    frame->setObjectName("frameless");
    frame->setStyleSheet(QR(":/frameless/qrc/frameless.css"));

    QBoxLayout *frameLayout = qobject_cast<QBoxLayout *>(frame->layout());
    frameLayout->insertWidget(0, m_widget);
    frameLayout->setContentsMargins(12, 0, 12, 12);

    QGraphicsDropShadowEffect *pShadow = new QGraphicsDropShadowEffect(frame);
    pShadow->setOffset(0, 0);
    pShadow->setColor(QColor(0, 0, 0, 120));
    pShadow->setBlurRadius(20);
    frame->setGraphicsEffect(pShadow);

    // title
    QString title_text = windowTitle();
    QIcon title_icon = windowIcon();
    setTitleText(title_text.isEmpty() ? qApp->applicationName() : title_text);
    setTitleIcon(title_icon.isNull() ? QPixmap() : title_icon.pixmap(30).scaled(QSize(30,30), Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

void FramelessDialog::setTitleIcon(const QString &icon)
{
    QPixmap pixmap(icon);

    ui->icon->clear();
    ui->icon->setPixmap(pixmap);
}

void FramelessDialog::setTitleIcon(const QPixmap &icon)
{
    ui->icon->setPixmap(icon);
}

void FramelessDialog::setTitleText(const QString &text)
{
    ui->title->setText(text);
}

